#@layout()
#define main()
	<div>
		<div style="margin:5px 0;border-bottom:1px solid #aaa;">
			<span class="layui-breadcrumb">
				<a href="#">系统运行情况</a>
				<a><cite>压力测试</cite></a>
			</span>
        </div>
		<form id="form1" class="layui-form" action="" method="post" style="padding:0px 0 0 20px;">
			<div class="layui-form-item">
				<div class="layui-inline col-24 col-sm-24"> 
					<label class="layui-form-label col-3 col-sm-6">测试url</label>
					<div class="layui-input-inline col-21 col-sm-16">
						<input type="text" id="url" lay-verify="" autocomplete="off" placeholder="请输入测试的Url" class="layui-input">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline col-24 col-sm-24"> 
					<label class="layui-form-label col-3 col-sm-6">参数值(json)</label>
					<div class="layui-input-inline col-21 col-sm-16">
						<input type="text" id="params" lay-verify="" autocomplete="off" placeholder="请输入参数值，格式json" class="layui-input">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline col-12 col-sm-24"> 
					<label class="layui-form-label col-6 col-sm-6">每秒测试次数</label>
					<div class="layui-input-inline col-18 col-sm-16">
						<input type="text" id="num" lay-verify="" autocomplete="off" placeholder="请输入代码值" class="layui-input">
					</div>
				</div>
				<div class="layui-inline col-12 col-sm-24"> 
					<label class="layui-form-label col-6 col-sm-6">测试时间(秒)</label>
					<div class="layui-input-inline col-18 col-sm-16">
						<input type="text" id="time" lay-verify="" autocomplete="off" placeholder="测试时间" class="layui-input">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline col-12 col-sm-24"> 
					<label class="layui-form-label col-6 col-sm-6">每秒提交次数</label>
					<div class="layui-input-inline col-18 col-sm-16">
						<input type="text" id="numSubmit" lay-verify="" autocomplete="off" placeholder="" readonly class="layui-input">
					</div>
				</div>
				<div class="layui-inline col-12 col-sm-24"> 
					<label class="layui-form-label col-6 col-sm-6">错误次数</label>
					<div class="layui-input-inline col-18 col-sm-16">
						<input type="text" id="errnum" lay-verify="" autocomplete="off" placeholder="" readonly class="layui-input">
					</div>
				</div>
			</div>
			<div class="layui-form-item" style="margin-bottom: 0;">
				<div class="layui-inline">
				<button id="demo1" class="layui-btn" lay-submit="" lay-filter="demo1">开始测试</button>
				</div>
			</div>
		</form>
		<script>

		layui.use(['form', 'laydate'], function(){
			var form = layui.form
			,layer = layui.layer;

			var ttt;
			//监听提交
			form.on('submit(demo1)', function(data){
					//layer.alert(JSON.stringify(data.field), { title: '最终的提交信息' });
					//return false;
				var $demo1=$("#demo1");
				var url=$("#url").val();
				var params=$("#params").val();
				var num=$("#num").val();
				var time=$("#time").val();
				var curTime=new Date().getTime();
				
				var count=0;
				var errnum=0;
				try{
					if(params!=null && params!="")
						params=JSON.parse(params);
				}catch(e){
					alert("参数格式错误，请使用JSON格式，例：{\"name\":\"huangxiaojian\",\"age\":\"23\"}");
					return false;
				}
				jQuery.support.cors = true;
				test=function(){
					if(new Date().getTime()-curTime>time*1000)
					{
						clearInterval(ttt);
						$demo1.html("开始测试");
						return;
					}
					for(var i=0;i<num;i++){
						$.ajax({url:url,
 							data:params,
 						  	type: 'post',
							error:function(XMLHttpRequest, textStatus){  
								errnum++;
						    }
						});  
						
						count++;
					}
					$("#errnum").val(errnum);
		    		$("#numSubmit").val(count);
				};
				
				if($demo1.html()=="开始测试"){
					$demo1.html("停止测试");
					ttt=setInterval(test,1000);
					
				}else{
					clearInterval(ttt);
					$demo1.html("开始测试");
				}
				return false;
			});
		});
		</script>
		
	</div>
#end